var log = new Logger();
var wsUri = "ws://" + document.location.host + document.location.pathname + "endpoint";
var webSocket = new WebSocket(wsUri);
var ctrlMessage = document.getElementById("txtMessage");

webSocket.onopen = function (message)
{
    processOpen(message);
}

webSocket.onclose = function (message)
{
    processClose(message);
}

webSocket.onerror = function (event)
{
    processError(error);
}

webSocket.onmessage = function (message)
{
    log.info("receive from server: " + message.data);
}

function processOpen(message)
{
    log.info("Connected to " + message);
}

function processClose(message)
{
    log.info("Sending disconnected message...");
    webSocket.send("Client disconnected");
    log.info("Connection closed")
}

function processError(event)
{
    log.error("Error on " + event);
}

function sendMessage()
{
    var message = ctrlMessage.value;
    log.info("Sending to server: " + message);
    webSocket.send(message);
    ctrlMessage.value = "";
}

function closeConnection()
{
    webSocket.close()
}